package com.example.utils;

import org.springframework.util.AntPathMatcher;

/**
 * 字符串工具类
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {
    
    /** 空字符串 */
    private static final String NULLSTR = "";

    /** 下划线 */
    private static final char SEPARATOR = '_';
    
    /** 路径匹配器 */
    private static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    /**
     * 截取字符串
     *
     * @param str 字符串
     * @param start 开始
     * @param end 结束
     * @return 结果
     */
    public static String substring(final String str, int start, int end) {
        if (str == null) {
            return NULLSTR;
        }

        if (end < 0) {
            end = str.length() + end;
        }
        if (start < 0) {
            start = str.length() + start;
        }

        if (end > str.length()) {
            end = str.length();
        }

        if (start > end) {
            return NULLSTR;
        }

        if (start < 0) {
            start = 0;
        }
        if (end < 0) {
            end = 0;
        }

        return str.substring(start, end);
    }

    /**
     * 截取字符串
     *
     * @param str 字符串
     * @param len 长度
     * @return 结果
     */
    public static String substring(final String str, int len) {
        if (str == null) {
            return NULLSTR;
        }
        if (len < 0) {
            return NULLSTR;
        }
        if (str.length() <= len) {
            return str;
        }
        return str.substring(0, len);
    }

    /**
     * 判断path是否匹配pattern
     *
     * @param pattern 匹配规则
     * @param path 路径
     * @return 是否匹配
     */
    public static boolean isPathMatch(String pattern, String path) {
        return PATH_MATCHER.match(pattern, path);
    }
} 